package com.certification.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.certification.common.utils.Constant;
import com.certification.dto.SchoolInvestigateDTO;
import com.certification.entity.SchoolInvestigate;
import com.certification.service.SchoolInvestigateService;
import com.certification.service.SysUserRoleService;
import com.certification.vo.FindCertVO;
import com.certification.vo.PageVO;
import com.certification.vo.ResponseMessage;
import com.certification.vo.SchoolInvestigateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-03-29 23:07
 * @function
 */
@RestController
@Api(tags = {"流程：进校考察"})
public class SchoolInvestigateController extends AbstractController {

    @Resource
    private SchoolInvestigateService schoolInvestigateService;

    @Resource
    private SysUserRoleService sysUserRoleService;

//    @PostMapping("invest")
//    @ApiOperation(value = "新增考察")
//    public ResponseMessage<String> addInvest(@RequestBody SchoolInvestigateDTO schoolInvestigateDTO) {
//        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
//            if (roleId == Constant.ROLE_PRINCIPAL) {
//                return schoolInvestigateService.addInvest(schoolInvestigateDTO, Math.toIntExact(getUserId()));
//            }
//        }
//        return ResponseMessage.errorMsg("该角色不能调用该接口~");
//    }

    @PostMapping("invest")
    @ApiOperation(value = "新增考察")
    public ResponseMessage<String> addInvest(@RequestBody List<SchoolInvestigateDTO> schoolInvestigateDTOList) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return schoolInvestigateService.addInvestList(schoolInvestigateDTOList, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("investPage")
    @ApiOperation(value = "查看考察分页")
    public ResponseMessage<IPage<SchoolInvestigateVO>> getInvestPage(FindCertVO findCertVO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return schoolInvestigateService.getInvestPage(Math.toIntExact(getUserId()), findCertVO);
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("invest")
    @ApiOperation(value = "查看考察")
    public ResponseMessage<List<SchoolInvestigateVO>> getInvest() {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return schoolInvestigateService.getInvest(Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("invest/{investId}")
    @ApiOperation(value = "查看考察详情")
    public ResponseMessage<SchoolInvestigate> getInvestDetail(@PathVariable Integer investId) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return schoolInvestigateService.getInvestDetail(investId, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @PutMapping("invest")
    @ApiOperation(value = "修改考察")
    public ResponseMessage<SchoolInvestigate> getInvestDetail(@RequestBody SchoolInvestigateDTO schoolInvestigateDTO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return schoolInvestigateService.putInvest(schoolInvestigateDTO, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

}
